package com.biostime.qdingding.modules.qd.api;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.biostime.qdingding.common.config.Global;
import com.biostime.qdingding.common.web.BaseController;
import com.biostime.qdingding.modules.cms.entity.Article;
import com.biostime.qdingding.modules.cms.service.ArticleService;
import com.biostime.qdingding.modules.qd.api.dto.ActivityDetailDto;
import com.biostime.qdingding.modules.qd.api.dto.CommentDto;
import com.biostime.qdingding.modules.qd.api.dto.VersionUpdateDto;
import com.biostime.qdingding.modules.qd.entity.Activity;
import com.biostime.qdingding.modules.qd.entity.Album;
import com.biostime.qdingding.modules.qd.service.ActivityService;
import com.biostime.qdingding.modules.qd.service.AlbumService;
import com.biostime.qdingding.modules.qd.service.AppVersionService;
import com.biostime.qdingding.modules.qd.service.CommentService;

/**
 * 文章模块
 * 
 * @author chenweicheng
 * @version 2015-12-21
 */
@Controller
@RequestMapping(value = "${adminPath}/qd")
public class WwwApi extends BaseController{
	@Autowired
	ArticleService articleService;
	
	@Autowired
	private ActivityService activityService;
	
	@Autowired
	private AlbumService albumService;
	
	@Autowired
	private CommentService commentService;
		
	@Autowired
	private AppVersionService versionService;
	/**
	 * 轮播图跳转页面
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value="article/show/{id}")
	public String show(@PathVariable String id,Model model){
		try{
			Article article = articleService.get(id); 
			model.addAttribute("article", article);
		}catch(NullPointerException  e){
			model.addAttribute("msg", "文章不存在或参数错误");
		}
		return "modules/cms/articleShow";
	}
		
	/**
	 * 活动介绍
	 */	
	@RequestMapping(value="activity/show/{id}")
	public String activityContent(@PathVariable String id,Model model){
		if(!StringUtils.isEmpty(id)){
			Activity activity = activityService.get(id);
			if(activity != null){
			String content = activity.getContent();
				model.addAttribute("content", content);
			}else{
				model.addAttribute("content", "活动异常");
			}
		}else{
			model.addAttribute("content", "参数异常");
		}
		return "modules/qd/activityContent";
	}	
	
	/**
	 * 获取活动详情dto
	 * 用于ajax请求
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("activity/getActivityDto")
	public ActivityDetailDto getActivityDto(HttpServletRequest request){
		String activityId = request.getParameter("activityId");
	    String userId = request.getParameter("userId");
		ActivityDetailDto activity = activityService.apiGetActivity(activityId,userId);
		
		//用户id
		if(userId != null && !userId.equals("")){
			activity.setUserId(Integer.parseInt(userId));			
		}
		
		//获取收藏人的头像列表
		List<String> list = activityService.apiGetImgs(activityId);
		List<String> list2 = new ArrayList<String>();
		if(list != null && !list.isEmpty()){
			for(String str : list){
				str = "http://"+Global.getConfig("domain")+"/qdingding-webapp/cdn/image/"+str;
				list2.add(str); 
			}			
		}
		activity.setHeadImages(list2);
		
		return activity;
	}
		
	/**
	 * 获取活动详情
	 * @param id
	 * @return
	 */
	@RequestMapping("activity/activityDetail")
	public ModelAndView activityDetail(HttpServletRequest request){
	
		String activityId = request.getParameter("activityId");
		String userId = request.getParameter("userId");
		String type = request.getParameter("type");
		//获取活动详情
		ModelAndView modelAndView = new ModelAndView();
		ActivityDetailDto activity = activityService.apiGetActivity(activityId,userId);
		
		//获取收藏人的头像列表
		List<String> list = activityService.apiGetImgs(activityId);
		List<String> list2 = new ArrayList<String>();
		if(list != null && !list.isEmpty()){
			for(String str : list){
				str = "http://"+Global.getConfig("domain")+"/qdingding-webapp/cdn/image/"+str;
				list2.add(str);
			}			
		}
		activity.setHeadImages(list2);
		
		//获取收藏人数
		int number = activityService.apiGetNumber(activityId);
		activity.setCollectNumber(number); 
		
		//活动图片
		List<Album> album = albumService.apiGet("activity", activityId);
		if(album != null && !album.isEmpty()){
			if(activity != null){
				String str = album.get(0).getOrginPath();
				activity.setImage("http://"+Global.getConfig("domain")+"/qdingding-webapp/cdn/image/"
						+str);
			}
		}
		
		//评论列表
		List<CommentDto> commentList = commentService.apiActivityComments(activityId,userId);
		if(commentList != null){
			if(!commentList.isEmpty()){
				for(CommentDto dto : commentList){
					
					//评论图片列表
					List<Album> commentAlbum = albumService.apiGet("activityComment", String.valueOf(dto.getId()));	
					if(commentAlbum != null && !commentAlbum.isEmpty()){
						for(Album album1 : commentAlbum){
							album1.setOrginPath( "http://"+Global.getConfig("domain")+"/qdingding-webapp/"
									+ "cdn/image/"+album1.getOrginPath()); 		
							album1.setThumbPath( "http://"+Global.getConfig("domain")+"/qdingding-webapp/"
									+ "cdn/image/"+album1.getThumbPath()); 						
						}					
					}
					dto.setImages(commentAlbum); 
					
					//头像列表
					List<Album> headAlbum = albumService.apiGet("headImg", String.valueOf(dto.getUserId()));
					if(headAlbum != null && !headAlbum.isEmpty()){
						dto.setHeadImg("http://"+Global.getConfig("domain")+"/qdingding-webapp/cdn/image/"
								+headAlbum.get(0).getOrginPath()); 
					}
					
				}
			}else{
				commentList = null;
			}
		}
				
		//活动介绍url
		String projectUrl = request.getRequestURI();
		String url[] = projectUrl.split("/");
		if(url.length>3){
			projectUrl = url[0]+"/"+url[1]+"/"+url[2];
		}
		String serverUrl = "http://"+Global.getConfig("domain")+"/qdingding-webapp"+Global.getConfig("adminPath")+"/qd/activity/show/"+activity.getId();
		activity.setDetailUrl(serverUrl); 
		
		//是否为分享页面
		if(type != null){
			activity.setType(type); 
		}
		
		//用户id
		if(userId != null && !userId.equals("")){
			activity.setUserId(Integer.parseInt(userId));			
		}
		
		if(type.equals("share")){
			
			//苹果版本更新链接
			List<VersionUpdateDto>  dtos = versionService.apiGetVersion("ios");
			if(dtos != null && !dtos.isEmpty()){
				activity.setIosUpdateUrl(dtos.get(0).getUpdateUrl());
			}	
			
			//安卓版本更新链接
			List<VersionUpdateDto>  dto2s = versionService.apiGetVersion("android");
			if(dto2s != null && !dto2s.isEmpty()){
				activity.setAndroidUpdateUrl(dto2s.get(0).getUpdateUrl());
			}				
		}
		
		modelAndView.addObject("activity", activity);
		modelAndView.addObject("commentList",commentList);
		modelAndView.setViewName("modules/qd/activityDetail");
		
		return modelAndView;
	}
}
